package todolist.test;

import todolist.activities.LoginActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

public class JUnitLoginScreenActivity  extends ActivityInstrumentationTestCase2<LoginActivity> {

	private LoginActivity loginActivity;
	private EditText uname;
	private EditText pw;
	
	public JUnitLoginScreenActivity() {
		super("todolist.activities", LoginActivity.class);
	}
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		loginActivity = this.getActivity();
		uname = (EditText) loginActivity.findViewById(todolist.activities.R.id.log_username);
		pw = (EditText) loginActivity.findViewById(todolist.activities.R.id.log_password);
	}
	
	public void testPreconditions() {
		assertNotNull(uname);
		assertNotNull(pw);
	}

	public void testNullLogin() {
		assertTrue(loginActivity.checkForNull(uname, pw));
	}
	
	/*
	 * These tests achieve branch coverage for the LoginScreen. Null data is tested in both the
	 * username and password fields. In these cases check for null should return true because a null
	 * value is found. Assert false should only succeed when all fields have been filled out
	 */

	public void testCompleteData() {
		sendKeys("D");
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys("D");
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		assertFalse(loginActivity.checkForNull(uname, pw));
	}

	public void testEmptyPassword() {
		sendKeys("D");
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys("ENTER");
		assertTrue(loginActivity.checkForNull(uname, pw));
	}
	
	public void testEmptyUsername() {
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys("D");
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys("ENTER");
		assertTrue(loginActivity.checkForNull(uname, pw));
	}
	
	public void testInvalidLogin() {
		sendKeys("A");
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys("A");
		sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
		sendKeys("ENTER");
		assertTrue(loginActivity.isInvalidLogin());
	}
	
}
